package com.micro.school.edu.service.impl;
import java.util.Arrays;
import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.edu.convert.SchoolCampusConvert;
import com.micro.school.edu.domain.SchoolCampus;
import com.micro.school.edu.domain.vo.CollegeModelVo;
import com.micro.school.edu.domain.vo.SchoolCampusVo;
import com.micro.school.edu.mapper.SchoolCampusMapper;
import com.micro.school.edu.service.ISchoolCampusService;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.SchoolCampusVos;
import com.micro.school.system.api.domain.vo.SchoolCollegeVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 校区信息Service业务层处理
 *
 * @author rakic
 * @date 2025-08-06
 */
@Service
public class SchoolCampusServiceImpl implements ISchoolCampusService
{
    @Autowired
    private SchoolCampusMapper schoolCampusMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;

    /**
     * 查询校区信息
     *
     * @param id 校区信息主键
     * @return 校区信息
     */
    @Override
    public SchoolCampusVo selectSchoolCampusById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolCampusMapper.selectSchoolCampusById(id);
    }

    @Override
    public SchoolCampusVos selectSchoolCampusByIds(Long id) {
        return schoolCampusMapper.selectSchoolCampusByIds(id,SecurityContextHolder.getUserTenantId());
    }

    /**
     * 查询校区信息列表
     *
     * @param schoolCampusVo 校区信息
     * @return 校区信息
     */
    @Override
    public List<SchoolCampusVo> selectSchoolCampusList(SchoolCampusVo schoolCampusVo)
    {
        schoolCampusVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            schoolCampusVo.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
        }
        return schoolCampusMapper.selectSchoolCampusList(schoolCampusVo);
    }

    /**
     * 新增校区信息
     *
     * @param schoolCampusVo 校区信息
     * @return 结果
     */
    @Override
    public AjaxResult insertSchoolCampus(SchoolCampusVo schoolCampusVo)
    {
        if (schoolCampusVo.getContactPhone() != null && schoolCampusVo.getContactPhone().length() > 11){
            return AjaxResult.error("电话号码超长，请输入正确号码");
        }
        SchoolCampus schoolCampus = SchoolCampusConvert.INSTANCE.convert(schoolCampusVo);
        schoolCampus.setSchoolId(schoolCampusVo.getTenantId());
        schoolCampus.setSchoolName(schoolCampusVo.getSchoolName());
        schoolCampus.setCreateTime(DateUtils.getNowDate());
        schoolCampus.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolCampus.setSchoolName(studentInfoVos.getSchoolName());
        List<SchoolCampus> schoolCampusList = schoolCampusMapper.selectSchoolCampusByName(schoolCampusVo.getCampusName(),SecurityContextHolder.getUserTenantId());
        if (schoolCampusList != null && schoolCampusList.size() > 0){
            return AjaxResult.error("已有同名校区");
        }else{
            schoolCampusMapper.insertSchoolCampus(schoolCampus);
            return AjaxResult.success("插入成功");
        }

    }

    /**
     * 修改校区信息
     *
     * @param schoolCampusVo 校区信息
     * @return 结果
     */
    @Override
    public int updateSchoolCampus(SchoolCampusVo schoolCampusVo)
    {
        SchoolCampus schoolCampus = SchoolCampusConvert.INSTANCE.convert(schoolCampusVo);
        schoolCampus.setUpdateTime(DateUtils.getNowDate());
        SchoolCampusVo schoolCampusVo1 = schoolCampusMapper.selectSchoolCampusById(schoolCampusVo.getId());
        if (!schoolCampusVo.getCampusName().equals(schoolCampusVo1.getCampusName())){
            remoteUserService.updateAllCampusName(schoolCampusVo.getCampusName(),schoolCampusVo.getId(),schoolCampusVo1.getCampusName());
            remoteOrderService.updateAllCampusName(schoolCampusVo.getCampusName(),schoolCampusVo.getId());
        }
        return schoolCampusMapper.updateSchoolCampus(schoolCampus);
    }

    /**
     * 批量删除校区信息
     *
     * @param ids 需要删除的校区信息主键
     * @return 结果
     */
    @Override
    public int deleteSchoolCampusByIds(List<Long> ids)
    {
//        Integer.parseInt(SecurityContextHolder.getUserTenantId())
//        BeanUtils.checkIdsEmpty(ids);
//        schoolCampusMapper.deleteSchoolCampusByIds(ids);
        return schoolCampusMapper.updateSchoolCampusInfo(ids);
    }

    /**
     * 删除校区信息信息
     *
     * @param id 校区信息主键
     * @return 结果
     */
    @Override
    public int deleteSchoolCampusById(Long id)
    {
//        BeanUtils.checkIdsEmpty(id);
        return schoolCampusMapper.deleteSchoolCampusById(id);
    }

    @Override
    public List<SchoolCampusVos> selectSchoolCampusLists(SchoolCampusVos schoolCampusVos) {
        schoolCampusVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            schoolCampusVos.setCampusId(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0  ? SecurityContextHolderUtils.getCampusId():null);
        }
        return schoolCampusMapper.selectSchoolCampusLists(schoolCampusVos);
    }

    @Override
    public String getAllCampusIdVos(SchoolCampusVos schoolCampusVos) {
        schoolCampusVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolCampusVos.setDeleteFlag("0");
        List<String> collegeNames = schoolCampusMapper.getAllCampusIdVos(schoolCampusVos);
        return Arrays.toString(collegeNames.toArray());
    }

    @Override
    public List<SchoolCampusVos> selectSchoolCampusLists1(SchoolCampusVos schoolCampusVos) {
        return schoolCampusMapper.selectSchoolCampusLists(schoolCampusVos);
    }

}
